package kr.hkit.summary_java.serivceUI; 

import javax.swing.JPanel;

import kr.hkit.myframework.db.ConnectionFactory;
import kr.hkit.myframework.uicomp.InputCombo;
import kr.hkit.myframework.uicomp.InputPanel;
import kr.hkit.summary_java.dao.EmployeeDAO;
import kr.hkit.summary_java.erp.dto.Employee;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

public class PanelEmployeeUI extends JPanel implements ActionListener {
	private InputPanel pEmpNo;
	private InputPanel pEmpName;
	private InputCombo pTitle;
	private InputCombo pManager; 
	private InputPanel pSalary;
	private InputCombo pDeptNo; 
	
	private JButton btnAdd;
	private JButton btnClose;
	
	private String btnText;
	
	private EmployeeDAO dao_emp;
	private JPanel panel_Center;
	private JPanel panel_Bottom;
	
	
	
	public String getBtnAdd() {
		return btnAdd.getText();
	}

	public void setBtnAdd(String text) {
		btnAdd.setText(text);
		
	}

	public PanelEmployeeUI(String btnText, EmployeeDAO dao_emp) {
		this.btnText = btnText;
		this.dao_emp = dao_emp;
		initialize();
	}
	
	private void initialize() {
		setPreferredSize(new Dimension(500, 200));
		setLayout(new BorderLayout(0, 0));
		
		panel_Center = new JPanel();
		add(panel_Center, BorderLayout.CENTER);
		panel_Center.setLayout(new GridLayout(0, 1, 0, 0));
		
		pEmpNo = new InputPanel("사원번호");
		panel_Center.add(pEmpNo);
		pEmpNo.getTf().addFocusListener(new FocusAdapter() {

			@Override
			public void focusLost(FocusEvent e) {
				super.focusLost(e);

				int empno = Integer.parseInt(pEmpNo.getTf().getText().trim()); //
				Employee emp = new Employee();
				emp.setEmpNo(empno);

				try {
					emp = dao_emp.searchItem(emp);
					pEmpName.setTf(emp.getEmpName());
					pTitle.setDefaultSelectIndex(emp.getTitle()-1);
					pManager.setSelectItem(dao_emp.selectedEmpNameByEmpNo(emp.getManager()));
					pSalary.setTf(emp.getSalary() + "");
					pDeptNo.setDefaultSelectIndex(emp.getDeptNo()-1);
					
					if ((btnAdd.getText().equals("삭제"))
							|| (btnAdd.getText().equals("검색"))) {
						panelDisable();
					} else if (btnAdd.getText().equals("수정")){
						panelEnable2();
					}
					
				} catch (SQLException e1) {
					e1.printStackTrace();
				} catch (NullPointerException e1) {
					if (!btnAdd.getText().equals("추가")) {
						JOptionPane.showMessageDialog(PanelEmployeeUI.this,"해당 사원이 없습니다.");
					} else {
						JOptionPane.showMessageDialog(PanelEmployeeUI.this,"추가 가능합니다.");
					}
				}
			}
		});
		
		
		pEmpName = new InputPanel("사원명");
		panel_Center.add(pEmpName);
		
		pTitle = new InputCombo("직책"); //콤보박스
		String[] items_Title = null ;
		try {
			items_Title = dao_emp.getTitleitems();
			pTitle.addItems(items_Title);
		} catch (SQLException e2) {
			e2.printStackTrace();
		}
		pTitle.setSelectItem("사원"); 
		panel_Center.add(pTitle);

		
		pManager = new InputCombo("매니저"); //콤보박스
		String[] items_Manager ;
		try {
			items_Manager = dao_emp.getManagerItems();
			pManager.addItems(items_Manager);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		pManager.setSelectItem("해당없음"); 
		panel_Center.add(pManager);
		
		
		pSalary = new InputPanel("월급여");
		panel_Center.add(pSalary);
		
		
		pDeptNo = new InputCombo("부서명"); //콤보박스
		
		String[] items_DeptNo;
		try {
			items_DeptNo = dao_emp.getDeptItems();
			pDeptNo.addItems(items_DeptNo); 
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
	
		pDeptNo.setSelectItem("영업"); //default
		panel_Center.add(pDeptNo);
		
		
		panel_Bottom = new JPanel();
		add(panel_Bottom, BorderLayout.SOUTH);
		
		btnAdd = new JButton(btnText);
		btnAdd.addActionListener(this);
		panel_Bottom.add(btnAdd);
		
		
		if ((btnAdd.getText().equals("삭제")) || (btnAdd.getText().equals("검색"))) {
			panelDisable();
		}
		
		btnClose = new JButton("닫기");
		btnClose.addActionListener(this);
		panel_Bottom.add(btnClose);
	}

	
	private void panelDisable() {
		pEmpNo.getTf().setEnabled(true);
		pEmpName.getTf().setEnabled(false);
		pTitle.setEnableF();
		pManager.setEnableF();
		pSalary.getTf().setEnabled(false);
		pDeptNo.setEnableF(); 
	}
	
	private void panelEnable1() { 
		pEmpNo.getTf().setEnabled(true);
		pEmpName.getTf().setEnabled(true);
		pTitle.setEnableT();
		pManager.setEnableT();
		pSalary.getTf().setEnabled(true);
		pDeptNo.setEnableT();
	}
	
	private void panelEnable2() { //수정용
		pEmpNo.getTf().setEnabled(false);
		pEmpName.getTf().setEnabled(false);
		pTitle.setEnableT();
		pManager.setEnableT();
		pSalary.getTf().setEnabled(true);
		pDeptNo.setEnableT();
	}
	
	private void panelClear() {
		pEmpNo.getTf().setText("");
		pEmpName.getTf().setText("");
		pTitle.setSelectItem("사원");
		pManager.setSelectItem("해당없음");
		pSalary.getTf().setText("");
		pDeptNo.setSelectItem("영업");
	}

	
	private Employee getEmpAll() { //추가, 수정
		
		int empNo = Integer.parseInt(pEmpNo.getTf().getText().trim());
		String empName = pEmpName.getTf().getText();
		int title = pTitle.getSelectIndex()+1;
		int manager = 0;
		try {
			manager = dao_emp.selectedEmpNoByEmpName(pManager.getSelectItem());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		int salary = Integer.parseInt(pSalary.getTf().getText().trim());
		int deptno = pDeptNo.getSelectIndex()+1;
		
		return new Employee(empNo, empName, title, manager, salary, deptno);
		
	}
	
	
	private Employee getEmp() {
		int empNo = Integer.parseInt(pEmpNo.getTf().getText().trim());
		
		return new Employee(empNo);
	} 
	
	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnClose) {
			actionPerformedBtnClose(e);
		} else if (e.getActionCommand().equals("추가")) {
			actionPerformedBtnAdd(e);
		} else if (e.getActionCommand().equals("삭제")) {
			actionPerformedBtnDelete(e);
		} else if (e.getActionCommand().equals("수정")) {
			actionPerformedBtnUpdate(e);
		} else if (e.getActionCommand().equals("검색")) {
			actionPerformedBtnSearch(e);
		} 
	}
	

	private void actionPerformedBtnSearch(ActionEvent e) { //검색
		Employee emp = getEmp();
		
		try {
			JOptionPane.showMessageDialog(this, dao_emp.searchItem(emp));
			panelClear();
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "검색 실패! 다시 확인하세요.");
		}
	}
	

	private void actionPerformedBtnUpdate(ActionEvent e)  { //수정
		
		Employee emp = getEmpAll(); 
		JOptionPane.showMessageDialog(this, emp);
		
		try {
			dao_emp.updateItemByNo(emp);
			JOptionPane.showMessageDialog(this, "수정되었습니다.");
			panelClear(); 
			panelEnable1();
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "수정 실패! 다시 확인하세요.");
		} 
	}

	private void actionPerformedBtnDelete(ActionEvent e) { //삭제 
		Employee emp = getEmp();
		JOptionPane.showMessageDialog(this, emp);
		
		try {
			dao_emp.deleteItemByNo(emp);
			JOptionPane.showMessageDialog(this, "삭제되었습니다.");
			panelClear();
			panelDisable();
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "삭제 실패! 다시 확인하세요.");
		} 
	}

	protected void actionPerformedBtnAdd(ActionEvent e) { //추가
		Employee emp =  getEmpAll(); 
		JOptionPane.showMessageDialog(this, emp);
		
		try {
			dao_emp.insertItem(emp);
			JOptionPane.showMessageDialog(this, "입력되었습니다.");
			panelClear();
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "입력 실패! 다시 확인하세요.");
		} 
	}
	
	
	protected void actionPerformedBtnClose(ActionEvent e) { //닫기
		panel_Center.setVisible(false);
		panel_Bottom.setVisible(false);
	}
}
